﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wjtour.Common;
using Hjp.FrameWork.Common;
using Wjtour.BLL;
using System.Linq;
using TongCheng.API.OpenAPI.Request;
using TongCheng.API.OpenAPI.Facade;
using TongCheng.API.OpenAPI.Domain;

namespace Wjtour.Web.Front
{
    public partial class hotelview : BasePage
    {
        #region 固定分页参数
        protected int page = 1;
        protected int pagesize = 4;
        #endregion

        scene_bll _bll = new scene_bll();
        protected PaginationData<Wjtour.Domain.scene> data = new PaginationData<Domain.scene>();
        protected int type = 1;
        protected List<Wjtour.Domain.line> recommendLines = new List<Domain.line>();
        Wjtour.BLL.line_bll _linebll = new line_bll();
        protected List<Wjtour.Domain.article> trafficGuides = new List<Domain.article>();//旅游攻略
        Wjtour.BLL.article_bll _articleBll = new article_bll(); 
        protected int id = 0;
        protected HotelInfo hotel = new HotelInfo();
        protected List<HotelImage> hotelImageItems = new List<HotelImage>();
        protected List<TrafficInfo> trafficInfoItems = new List<TrafficInfo>();
        protected List<HotelRoomInfo> hotelRoomInfoItems = new List<HotelRoomInfo>();
        #region 参数
        protected string comeDate = DateTime.Now.ToString("yyyy-MM-dd");
        protected string leaveDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
        #endregion

        protected TextBox txtComeDate;
        protected TextBox txtLeaveDate;
        protected TextBox txtLocation;
        protected TextBox txtHotelName;
        protected TextBox txtSceneryName;
        protected Button hotelSearch, scenerySearch;

        protected TextBox txtComeDate2;
        protected TextBox txtLeaveDate2;
        protected Button btnSearch2;

        protected void Page_Load(object sender, EventArgs e)
        {
            page = Utils.StrToInt(Request["page"], 1);
            id = Utils.StrToInt(Request["id"], 1);
            hotelSearch.Click += hotelSearch_Click;
            scenerySearch.Click += scenerySearch_Click;
            btnSearch2.Click += btnsearch_Click;
            bindList();
            if (!IsPostBack)
            {
                txtComeDate2.Text = comeDate;
                txtLeaveDate2.Text = leaveDate;
            }
            if (id > 0)
            {
                getHotelDetail(id);
            }
        }

        void scenerySearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSceneryName.Text))
            {
                string query = "s=" + HttpUtility.UrlEncode(txtSceneryName.Text);
                Response.Redirect("ticketlist.aspx?" + query);
            }
            Response.Redirect("ticketlist.aspx");
        }

        void hotelSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtComeDate.Text))
            {
                Utils.Alert(this.Page, "请选择入住日期！");
                return;
            }
            if (string.IsNullOrEmpty(txtLeaveDate.Text))
            {
                Utils.Alert(this.Page, "请选择离店日期！");
                return;
            }
            DateTime dt1 = DateTime.Now;
            DateTime dt2 = DateTime.Now.AddDays(1);
            DateTime.TryParse(txtComeDate.Text, out dt1);
            DateTime.TryParse(txtLeaveDate.Text, out dt1);
            if (dt1 > dt2)
            {
                Utils.Alert(this.Page, "入住日期不能晚于离店日期！");
                return;
            }

            string query = string.Format("cd={0}&ld={1}&loc={2}&n={3}", txtComeDate.Text, txtLeaveDate.Text, txtLocation.Text, txtHotelName.Text);
            Response.Redirect("hotellist.aspx?" + query);
        }

        void btnsearch_Click(object sender, EventArgs e)
        {
            getRooms();
        }

        void getRooms()
        {
            comeDate = txtComeDate2.Text;
            leaveDate = txtLeaveDate2.Text;

            GetHotelRoomsRequest request4 = new GetHotelRoomsRequest();
            request4.Body.clientIp = "172.0.0.1";
            request4.Body.hotelId = id;
            request4.Body.comeDate = comeDate;
            request4.Body.leaveDate = leaveDate;

            var response4 = HotelFacade.GetHotelRooms(request4);
            var HotelRoomList = response4.Body.hotelRoomList;
            hotelRoomInfoItems = response4.Body.hotelRoomList.Items;
        }

        void getHotelDetail(int hotelId)
        {
            GetHotelDetailRequest request = new GetHotelDetailRequest();
            request.Body.clientIp = "172.0.0.1";
            request.Body.hotelId = id;

            var response = HotelFacade.GetHotelDetail(request);
            hotel = response.Body.hotelInfo;


            GetHotelTrafficInfoRequest request2 = new GetHotelTrafficInfoRequest();
            request2.Body.clientIp = "172.0.0.1";
            request2.Body.hotelId = id;

            var response2 = HotelFacade.GetHotelTrafficInfo(request2);
            var trafficInfoList = response2.Body.trafficInfoList;
            trafficInfoItems = trafficInfoList.Items;

            GetHotelImageListRequest request3 = new GetHotelImageListRequest();
            request3.Body.clientIp = "172.0.0.1";
            request3.Body.hotelId = id;
            request3.Body.page = 1;
            request3.Body.pageSize = 12;

            var response3 = HotelFacade.GetHotelImageList(request3);
            var hotelImageList = response3.Body.hotelImageList;
            hotelImageItems = hotelImageList.Items;
            
            getRooms();
        }

        void bindList()
        {
            data = _bll.GetList(null, type.ToString(), page - 1, pagesize);
            PagnationHtml = GetPageHtml("sceniclist.aspx", page, data.Pager.TotalPages);

            recommendLines = _linebll.GetAll<Wjtour.Domain.line>("select top 6 * from line where isrecommend=1").ToList();
            trafficGuides = _articleBll.GetAuditTopListWithFirstLogo("6", 7, 2);
        }
    }
}
